<?php

namespace App\HttpController\Admin;

use App\Model\PhotoModel;

class PhotoController extends AuthController
{
    public function bingbing()
    {
        $request = $this->request();
        if ($request->getMethod() == 'POST') {
            $this->update();

            return $this->writeJson(0, null,'更新成功');
        } else {
            return $this->writeJson(0, $this->getData('bingbing'),'');
        }
    }

    /**
     * 更新
     */
    public function update(){
        $request = $this->request();
        $params = $request->getRequestParam();

        $newsList = [];
        foreach ($params['list'] as $data) {
            $sort = $data['sort'];
            unset($data['sort']);
            $newsList[$sort] = $data;
        }
        ksort($newsList);

        $PhotoModel = new PhotoModel();
        $params['index'] = json_encode($params['index'], JSON_UNESCAPED_UNICODE);
        $params['list'] = json_encode($newsList, JSON_UNESCAPED_UNICODE);
        $params['music'] = $params['music'];

        return $PhotoModel->update($params);
    }

    public function getData($name)
    {
        $PhotoModel = new PhotoModel();
        $data = $PhotoModel->where('name', $name)->first();
        $list = json_decode($data['list'], true);
        foreach ($list as $k => $val) {
            $list[$k]['sort'] = $k;
        }
        $data['list'] = $list;

        return $data;
    }
}
